package com.example.PDFdemo.Test;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PDFUtil {

    public static void fillPdf(String oldFilePath, String newFilePath, List<FillInfo> fillInfoList) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(oldFilePath);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(newFilePath));
        for (FillInfo fillInfo : fillInfoList) {
            if (!fillInfo.getType().equals("image")) {
                //设置基础字体
                String fontPath = "static/" + fillInfo.getFontName() + ".ttf";
                //颜色16进制转RGB
                Map<String, Integer> RGB = com.example.PDFdemo.pdffinalplan.PDFUtil.hex2RGB(fillInfo.getColor());
                BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseColor color = new BaseColor(RGB.get("red"), RGB.get("green"), RGB.get("blue"));
                //字体大小需要转换，itextpdf的字体大小单位为pt，前端的传入数据单位为px 1pt = 1px * 3 / 4
                Font font = new Font(baseFont, fillInfo.getFontSize() * 3 / 4);
                //字体颜色
                font.setColor(color);
                //设置字体样式   加粗 0不加粗，1加粗，默认不加粗   倾斜 0不倾斜，2倾斜，默认不倾斜
                font.setStyle(fillInfo.getFontBold() + fillInfo.getFontIncline());
                //添加文本
                PdfContentByte over = stamper.getOverContent(fillInfo.getPageNumber());
                ColumnText columnText = new ColumnText(over);
                //使用表格+单元格的实行能够实现文字在垂直方向的位置设置
                Rectangle pageRect = new Rectangle(reader.getPageSizeWithRotation(fillInfo.getPageNumber()));
                float x = pageRect.getRight();
                float y = pageRect.getTop();
                Rectangle textRect = new Rectangle(fillInfo.getX() * x, fillInfo.getY() * y, fillInfo.getX() * x +fillInfo.getW() * x, fillInfo.getY() * y + fillInfo.getH() * y);
                Paragraph paragraph =new Paragraph(fillInfo.getContent(), font);
                PdfPTable table = new PdfPTable(1);
                table.setTotalWidth(textRect.getWidth());
                //设置单元格在表格中的宽度占比，默认为80%
                table.setWidthPercentage(100);
                table.setSplitLate(false);
                PdfPCell cell = new PdfPCell(paragraph);
                cell.setFixedHeight(textRect.getHeight());
                cell.setPadding(0);
                if (fillInfo.getType().equals("multText")) {
                    cell.setVerticalAlignment(fillInfo.getVertical());
                }
                cell.setHorizontalAlignment(fillInfo.getHorizontal());
                //消除表格边框
                cell.disableBorderSide(15);
                table.addCell(cell);
                columnText.setSimpleColumn(textRect);
                columnText.addElement(table);
                columnText.go();
            } else {
                PdfContentByte over = stamper.getOverContent(fillInfo.getPageNumber());
                //创建矩形
                Rectangle rect = new Rectangle(reader.getPageSizeWithRotation(fillInfo.getPageNumber()));
                float x = rect.getRight();
                float y = rect.getTop();
                //获取图片
                Image image = Image.getInstance(fillInfo.getImgUrl());
                //添加图片
                over.saveState();
                PdfGState state = new PdfGState();
                // 设置填充透明度
                state.setFillOpacity(1.0f);
                over.setGState(state);
                // pdf 图层图片渲染
                over.addImage(image, x * fillInfo.getW(), 0f, 0f, y * fillInfo.getH(), fillInfo.getX() * x, fillInfo.getY() * y);
                over.restoreState();
            }
        }
        stamper.close();
        reader.close();
    }

    public static Map<String, Integer> hex2RGB(String hexStr){
        if(hexStr != null && !"".equals(hexStr) && hexStr.length() == 7){
            Map<String, Integer> rgb = new HashMap();
            rgb.put("red", Integer.valueOf(hexStr.substring( 1, 3 ), 16));
            rgb.put("green", Integer.valueOf(hexStr.substring( 3, 5 ), 16));
            rgb.put("blue", Integer.valueOf(hexStr.substring( 5, 7 ), 16));
            return rgb;
        }
        return null;
    }

}
